package com.settlement.system.service.csv;

import com.settlement.system.model.entity.common.TblOrderAuditCategory;
import com.settlement.system.model.entity.common.TblOrderAuditReport;
import com.settlement.system.model.entity.common.TblOrderCloseAccount;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblOrderCloseAccountService;
import com.settlement.system.common.util.common.OrderAuditCategoryCalculatorUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单稽核导出
 *
 * @author Ximi
 * @since 2020/10/24
 */
@Slf4j
public class OrderAuditCsvDatas extends BaseServiceCsvDatas {

    private ITblOrderCloseAccountService orderCloseAccountService;
    private List<TblOrderAuditCategory> categories;


    public OrderAuditCsvDatas(IBaseService baseService, Map<String, Object> params,
                              ITblOrderCloseAccountService orderCloseAccountService,
                              List<TblOrderAuditCategory> categories) {
        super(baseService, params);
        this.orderCloseAccountService = orderCloseAccountService;
        this.categories = categories;
    }

    @Override
    protected List<Map<String, Object>> getListParams(Map<String, Object> params) {
        try {
            List<Map<String, Object>> list = new ArrayList<>();
            List<TblOrderCloseAccount> closeAccounts = orderCloseAccountService.selectByParam(params);
            for (TblOrderCloseAccount closeAccount : closeAccounts) {
                HashMap<String, Object> map = (HashMap<String, Object>) ((HashMap<String, Object>) params).clone();
                map.put("closeAccountId", closeAccount.getId());
                list.add(map);
            }
            return list;
        } catch (Exception ex) {
            log.error("拆分订单稽核条件异常", ex);
        }
        return super.getListParams(params);
    }

    @Override
    public Object beforeWrite(Object data) {
        if (data instanceof TblOrderAuditReport) {
            TblOrderAuditReport report = (TblOrderAuditReport) data;
            TblOrderAuditCategory category = OrderAuditCategoryCalculatorUtils.selectCategory(report, params, categories);
            if (category != null) {
                report.setOrderAuditMainTypeName(category.getMainCategoryName());
                report.setOrderAuditDtlTypeName(category.getDetailCategoryName());
                report.setIsPush(category.getIsPush());
            }
            return report;
        }
        return data;
    }
}
